var userId = GetQueryString("userId");
if (!userId) {
    window.history.back(-1);
}
//声明全局图片数组变量
var imgArray=[]
var imgArray1=[]
//纬度
var lat=""
//经度
var lng=""
//详细地址
var adress=""

//位置信息回显
function showBackMap(a,g) {
    lat=a
    lng=g
}
function showBackAdress(detailadress) {
    adress=detailadress
}

$(function() {
    queryShopType()

    //打开地址定位
    $('#shopAdress').on('click',function(){
        $(".js_map iframe").prop('src','showMapinfo.html')
        $('.js_map').show()
    });
    //地图确定地理位置
    $('#confirm').on('click',function(){
        ue.clearStorage()
        $("#shopLatitude").val(lat)
        $("#shopLongitude").val(lng)
        $("#shopAdress").val(adress)
        $('.js_map').hide()
    });
    //地图取消定位查询
    $('#quit').on('click',function(){
        ue.clearStorage()
        $('.js_map').hide()
    });

    /*协议弹框确认按钮*/
    $('#confirm123').on('click',function(){
        $('#weuiAgree').prop("checked", true);
        $('.js_xieyi').hide()
    });

    //协议弹框取消按钮
    $('#quit123').on('click',function(){
        $('.js_xieyi').hide()
    });

    //提交审核
    $(document).on("click", "#showTooltips", function() {
        if (!$("#weuiAgree").is(':checked')) {
            $.toast("请勾选同意相关条款", "forbidden");
            return false;
        }
        var formData = new FormData();
        if ($("#shopName").val()){
            formData.append("shopName",$("#shopName").val())
        }else {
            $.toast("店铺名称不能为空","forbidden");
            return
        }
        if ($("#shopTel").val()){
            formData.append("shopTel",$("#shopTel").val())
        }else {
            $.toast("手机号不能为空","forbidden");
            return
        }
        if ($("#shopTypeId").val()){
            formData.append("shopTypeId",$("#shopTypeId").val())
        }else {
            $.toast("请选择商铺类型","forbidden");
            return
        }
        if ($("#discountRatio").val()){
            formData.append("discountRatio",$("#discountRatio").val())
        }else {
            $.toast("请选择折扣比例","forbidden");
            return
        }
        if ($("#shopAdress").val()){
            formData.append("shopAdress",$("#shopAdress").val())
        }else {
            $.toast("详细地址不能为空","forbidden");
            return
        }
        if ($("#shopLatitude").val()||$("#shopLongitude").val()){
            formData.append("shopLatitude",$("#shopLatitude").val())
            formData.append("shopLongitude",$("#shopLongitude").val())
        }else {
            $.toast("定位有误","forbidden");
            return
        }
        if (imgArray.length==0){
            //无营业执照
            $.toast("请上传营业执照","forbidden");
            return
        }else {
            for(var i=0;i<imgArray.length;i++) {
                formData.append("SellImgss",imgArray[i])
            }
        }
        if (imgArray1.length==0){
            //无营业执照
            $.toast("请上传门头照片","forbidden");
            return
        }else {
            for(var i=0;i<imgArray1.length;i++) {
                formData.append("HeadImgss",imgArray1[i])
            }
        }
        formData.append("wxUserId",userId)
        $('#showTooltips').addClass('weui-btn_disabled')
        var url = getWeiContextPath() + "/wx/shop/applyShop";
        $.ajax({
            url: url,
            type: 'post',
            data: formData,
            processData: false,
            contentType: false,
            dataType: "json",
            beforeSend:function(){
                $.showLoading("数据上传中")
            },
            success: function(res) {
                $.hideLoading()
                if (res.code==0){
                    // 申请成功
                    $.toast(res.msg,function () {
                        //返回上一页既用户中心
                        //window.location.href = "me.html?userId="+userId
                        window.location.replace("me.html?userId="+userId)

                    });
                }else {
                    $('#showTooltips').removeClass("weui-btn_disabled")
                    $.toast(res.msg,"forbidden");
                }
            }
        });

    });

    //营业执照的上传方法
    var tmpl = '<li class="weui-uploader__file" style="background-image:url(#url#);position: relative;">' +
        '<span class="del" style="width:20px;height:20px;position: absolute;top:-10px;right:-10px;border-radius: 50%;' +
        'display: block;background:url(images/cha.png)no-repeat center;background-size:100% 100%;"></span></li>',
        $uploaderInput = $("#uploaderInput"),
        $uploaderFiles = $("#uploaderFiles");

    $uploaderInput.on("change", function(e) {
        //填充图片数组
        for (var nu=0;nu<$("#uploaderInput")[0].files.length;nu++){
            imgArray.push($("#uploaderInput")[0].files[nu])
        }
        var src, url = window.URL || window.webkitURL || window.mozURL,
            files = e.target.files;
        for (var i = 0, len = files.length; i < len; ++i) {
            var file = files[i];
            if (url) {
                src = url.createObjectURL(file);
            } else {
                src = e.target.result;
            }
            $uploaderFiles.append($(tmpl.replace('#url#', src)));
        }
    });
    //营业执照的图片删除
    $(document).on('click', '.weui-uploader__file .del', function(event) {
        event.stopPropagation();
        imgArray.remove($(this).parents('li').index())
        $(this).parents('li').remove();
    })

    //门头照片的上传方法
    var tmpl1 = '<li class="weui-uploader__file" style="background-image:url(#url#);position: relative;">' +
        '<span class="del1" style="width:20px;height:20px;position: absolute;top:-10px;right:-10px;border-radius: 50%;' +
        'display: block;background:url(images/cha.png)no-repeat center;background-size:100% 100%;"></span></li>',
        $uploaderInput1 = $("#uploaderInput1"),
        $uploaderFiles1 = $("#uploaderFiles1");

    $uploaderInput1.on("change", function(e) {
        //填充图片数组
        for (var nu=0;nu<$("#uploaderInput1")[0].files.length;nu++){
            imgArray1.push($("#uploaderInput1")[0].files[nu])
        }

        var src, url = window.URL || window.webkitURL || window.mozURL,
            files = e.target.files;
        for (var i = 0, len = files.length; i < len; ++i) {
            var file = files[i];
            if (url) {
                src = url.createObjectURL(file);
            } else {
                src = e.target.result;
            }
            $uploaderFiles1.append($(tmpl1.replace('#url#', src)));
        }
    });
    $(document).on('click', '.weui-uploader__file .del1', function(event) {
        event.stopPropagation();
        imgArray1.remove($(this).parents('li').index())
        $(this).parents('li').remove();
    })
});

function queryShopType() {
    $.post(getWeiContextPath()+"/wx/data/selectShopTypeList",function (res) {
        $.each(res,function (i, obj) {
            $("#shopTypeId").append('<option value="'+obj.id+'">'+obj.typeName+'</option>')
        })
    })
}

/*查看协议*/
function seeXieYi() {
    $(".js_xieyi iframe").prop('src', 'xieyi.html')
    $('.js_xieyi').show()
}








